package utils

import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"os/exec"
	"strconv"
)

func Md5(file []byte) string {
	md5 := md5.New()
	// file, _ := ioutil.ReadFile("file_path")
	md5.Write(file) // 传入的是[]byte
	// io.Copy(md5, file)   // 传入的是io.Reader
	MD5Str := hex.EncodeToString(md5.Sum(nil))
	return MD5Str
}

func GetFrame(filename string, index int) *bytes.Buffer {
	if index < 1 {
		index = 1
	}
	cmd := exec.Command("ffmpeg", "-i", filename, "-vframes", strconv.Itoa(index), "-f", "singlejpeg", "-")
	// cmd := exec.Command("ffmpeg", "-i", filename, "-vframes", "1", "-s", fmt.Sprintf("%dx%d", width, height), "-f", "singlejpeg", "-")

	buf := new(bytes.Buffer)

	cmd.Stdout = buf

	if cmd.Run() != nil {
		panic("could not generate frame")
	}

	return buf
}
